<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="fi.foyt.cs.controller.UserController"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<%@page import="fi.foyt.cs.persistence.domainmodel.oauth.AuthorizedClient"%>
<%@page import="fi.foyt.cs.controller.OAuthController"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.system.Property"%>
<%@page import="fi.foyt.cs.controller.SystemController"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.task.GeneralTask"%>
<%@page import="fi.foyt.cs.controller.TaskController"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.task.TaskType"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <jsp:include page="../fragments/head.jsp"></jsp:include>
</head>
<body>
	<jsp:include page="../fragments/header.jsp"></jsp:include>

	<%
	  int resultsPerPage = 20;
	
    UserController userController = new UserController();
  
	  String startParameter = request.getParameter("start");
	  int firstResult = (startParameter != null) && (!"".equals(startParameter)) ? new Integer(startParameter) : 1;

	  List<Integer> setStarts = new ArrayList<Integer>(); 
    Integer userCount = userController.countUsers();
    for (int i = 0; i < userCount; i += resultsPerPage) {
      setStarts.add(i + 1);
    }
    
	  pageContext.setAttribute("users", userController.listUsers(firstResult - 1, resultsPerPage));
	  pageContext.setAttribute("resultsPerPage", resultsPerPage);
	  pageContext.setAttribute("userCount", userCount);
    pageContext.setAttribute("setStarts", setStarts);
    pageContext.setAttribute("currentStart", firstResult);
	%>

	<h1>Users</h1>

	<div style="max-width: 800px; margin-top: 8px; margin-bottom: 16px;">
    <c:forEach var="start" items="${setStarts}">
      <c:choose>
        <c:when test="${(start + resultsPerPage) < userCount}">
          <c:set var="last">${start + resultsPerPage}</c:set>
        </c:when>
        <c:otherwise>
          <c:set var="last">${userCount}</c:set>
        </c:otherwise>
      </c:choose>
      
      <c:choose>
        <c:when test="${start == currentStart}">
          <a href="?start=${start}" style="white-space: nowrap; width: 70px; display: inline-block; text-align: center; border: 1px solid #6C8C97; color: #fff; background: #8CACB7">
            ${start} - ${last}
          </a>
        </c:when>
        <c:otherwise>
          <a href="?start=${start}" style="white-space: nowrap; width: 70px; display: inline-block; text-align: center; padding: 1px; color: #000">
            ${start} - ${last}
          </a>
        </c:otherwise>
      </c:choose>
    </c:forEach>
  </div>
  
  <table>
    <tr>
      <th>Id</th>
      <th>Name</th>
      <th>Email</th>
      <th></th>
    </tr>
    <c:forEach items="${users}" var="user">
      <tr>
        <td>${user.id.id}</td>
        <td>${user.firstName} ${user.lastName}</td>
        <td>${user.email}</td>
        <td><a href="${pageContext.request.contextPath}/admin/users/deleteuser.jsp?userId=${user.id.id}">Delete</a></td>
      </tr>
    </c:forEach>
  </table>

	<jsp:include page="../fragments/footer.jsp"></jsp:include>
</body>
</html>